const app = require("express")();
const {
	addOne,
	addMany,
	deleteOne,
	deleteMany,
	updateOne,
	updateMany,
	queryOne,
	queryMany
} = require("../models/user/index");

/**
 * POST /api/user/add
 * @tags 用户
 * @summary 添加用户
 * @description 添加一个用户
 */
app.post("/add", async (req, res) => {
	try {
		const parmas = req.body;
		const result = await addOne(parmas, res);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});

/**
 * POST /api/user/addMany
 * @tags 用户
 * @summary 添加用户
 * @description 添加多个用户
 */
app.post("/addMany", async (req, res) => {
	try {
		const parmas = req.body;
		const result = await addMany(parmas, res);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});

/**
 * POST /api/user/delete
 * @tags 用户
 * @summary 删除用户
 * @description 删除一个用户
 */
app.post("/delete", async (req, res) => {
	try {
		const parmas = req.body;
		const id = { _id: parmas };
		const result = await deleteOne(id);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});
/**
 * POST /api/user/deleteMany
 * @tags 用户
 * @summary 删除用户
 * @description 删除多个用户
 */
app.post("/deleteMany", async (req, res) => {
	try {
		const parmas = req.body;
		// parmas:['11','22']
		const ids = { _id: { $in: parmas } };
		const result = await deleteMany(ids);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});
/**
 * POST /api/user/update
 * @tags 用户
 * @summary 更新用户
 * @description 更新一个用户
 */
app.post("/update", async (req, res) => {
	try {
		const parmas = req.body;
		// { title: "Mongoose Guide" }, //筛选条件
		// { title: "Mongoose Guide Updated" } //更新的数据
		const result = await updateOne({ _id: parmas._id }, parmas);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});
/**
 * POST /api/user/updateMany
 * @tags 用户
 * @summary 更新用户
 * @description 更新多个用户
 */
app.post("/updateMany", async (req, res) => {
	try {
		const parmas = req.body;
		// { title: "Mongoose Guide" }, //筛选条件
		// { title: "Mongoose Guide Updated" } //更新的数据
		const result = await updateMany(parmas.filter, parmas.data);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});

/**
 * POST /api/user/query
 * @tags 用户
 * @summary 查询用户信息
 * @description 查询一条用户信息
 */
app.post("/query", async (req, res) => {
	try {
		const parmas = req.body;
		const result = await queryOne(parmas);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}

});

/**
 * POST /api/user/queryMany
 * @tags 用户
 * @summary 查询用户信息
 * @description 查询多条用户信息
 */
app.post("/queryMany", async (req, res) => {
	try {
		const parmas = req.body;
		const result = await queryMany(parmas);
		res.API(result);
	} catch (err) {
		res.API_ERROR(err.msg, 1002, err?.mapped?.());
	}
});

module.exports = app;
